package com.tchhy.mvplibrary

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import com.example.mvpframe.mvplibrary.rx.DefaultEmptyObserver
import com.example.mvpframe.mvplibrary.rx.DefaultObserver
import com.tchhy.mvplibrary.rx.*
import com.tchhy.provider.SchedulerUtils
import com.tchhy.provider.data.common.BaseResp
import com.tchhy.provider.data.common.EmptyResp
import com.tchhy.provider.data.partner.response.DataListRes
import io.reactivex.Observable
import io.reactivex.Single

/**
 * 拓展Observale订阅方法（subscribe）,并在其中默认切换线程，包括异常通过处理
 */
@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<T>>.submit(observer: DefaultObserver<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    } else {
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}
@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<T>>.submit(observer: DefaultObserverForExpertCheck<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    } else {
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}
@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<T>>.submit(observer: ServiceObserver<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    } else {
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}
@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<T>>.submit(observer: DefaultObserverNoLoading<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    } else {
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}

@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<T>>.submit(observer: DefaultObserverNoToast<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    } else {
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}

@SuppressLint("CheckResult")
fun Observable<EmptyResp>.submit(observer: DefaultEmptyObserver) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    }else{
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }

}

@SuppressLint("CheckResult")
fun <T> Observable<BaseResp<DataListRes<T>>>.submit(observer: DefaultListObserver<T>) {
    if (!isNetWorkConnected(observer.context)) {
        observer.onErrorMessage("网络已断开，请检查网络连接")
    }else{
        this.compose(SchedulerUtils.io2Main())
            .compose(observer.lifecycleProvider.bindToLifecycle())
            .subscribe(observer)
    }
}

/**
 * 拓展Observale订阅方法（subscribe）,并在其中默认切换线程，包括异常通过处理
 */
@SuppressLint("CheckResult")
fun <T> Single<T>.submit(observer: DefaultSingleObserver<T>) {
    this.compose(SchedulerUtils.io2Main())
        .compose(observer.lifecycleProvider.bindToLifecycle())
        .subscribe(observer)
}

fun isNetWorkConnected(context: Context?): Boolean {
    if (context != null) {
        val mConnectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val mNetworkInfo = mConnectivityManager.activeNetworkInfo
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable && mNetworkInfo.isConnected
        }
    }

    return false
}
